//
//  InternationalControl.m
//  InternationalLanguage
//
//  Created by Wayhouse on 16/1/16.
//  Copyright © 2016年 Wayhouse. All rights reserved.
//

#import "InternationalControl.h"

@implementation InternationalControl

static NSBundle *bundle = nil;
+ (NSBundle *) bundle{
    return bundle;
}

+ (void) initInternationalLanguage{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *string;
    
        //in Chinese TW&HK area we all should use "zh-Hant"
        //在中国的台湾和香港地区, 都要用中文繁体 "zh-Hant"
        NSArray* languages = [def objectForKey:@"AppleLanguages"];
        NSString *current = [languages objectAtIndex:0];
        NSLog(@"current = %@",current);
        NSMutableArray * temp = (NSMutableArray*)[current componentsSeparatedByString:@"-"];
        if ([temp.firstObject isEqualToString:@"zh"]) {
            string = @"zh";
        }else{
            string = @"zh";
        }
    
    if([self currentLanguage] == nil)
    {
        [self setInternationalLanguage:string];
    }else
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:[self currentLanguage] ofType:@"lproj"];
        bundle = [NSBundle bundleWithPath:path];
    }
  
}

+(NSString *)currentLanguage{
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString* languages = [def objectForKey:@"tkuserLanguage"];
    return languages;
    
}


+(NSString *)language{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *language = [def valueForKey:@"tkuserLanguage"];
    return language;
}

+ (void)setInternationalLanguage:(NSString *) language{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj" ];
    bundle = [NSBundle bundleWithPath:path];
    [def setValue:language forKey:@"tkuserLanguage"];
    [def synchronize];
}


+ (NSString *) string:(NSString *) str{
// here the table name must as same as the .string file's name - 注意,这里必须和设置的.string文件名一致
    return [[self bundle] localizedStringForKey:str value:nil table:@"Localizable"];
}

@end
